/*
 * Copyright (c) 2010-2013 Mark Allen.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.faceboob.api.restfb.example;

import static java.lang.String.format;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;

import java.util.Date;

import com.restfb.BinaryAttachment;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.FacebookType;

/**
 * Examples of RestFB's Graph API functionality.
 * 
 * @author <a href="http://restfb.com">Mark Allen</a>
 */
public class GraphPublisherExample extends Example {
	/**
	 * RestFB Graph API client.
	 */
	private final FacebookClient facebookClient;

	/**
	 * Entry point. You must provide a single argument on the command line: a valid Graph API access token. In order for publishing to
	 * succeed, you must use an access token for an application that has been granted stream_publish and create_event rights.
	 * 
	 * @param args
	 *            Command-line arguments.
	 * @throws IllegalArgumentException
	 *             If no command-line arguments are provided.
	 */
	public static void main(final String[] args) {
		// if (args.length == 0)
		// throw new IllegalArgumentException(
		// "You must provide an OAuth access token parameter. "
		// + "See README for more information.");

		new GraphPublisherExample(
				"BAABZBxQZCZAlBwBAFi7AXis144EYmCPYMMuZCfow4fuOjTCpdFJiaRtei1SNr24nslOZCQpnM14D32kGFSZB0OE5ZCMZBG3oCHgImu6dZCZCdIg4W63CVWYi7aBulBnJipuvZAhtRyHUJwSRKlYzsVdQY9Hh0wYmiIinEnRnWl1Vfr0o2UiLK2WsqGvP8HPmyKi5Xhci6sLIr6c7vCJ3ZAU223neHfSqFiZBsomsZD")
				.runEverything();
	}

	GraphPublisherExample(final String accessToken) {
		this.facebookClient = new DefaultFacebookClient(accessToken);
	}

	void runEverything() {
		this.publishMessage();
	}

	String publishMessage() {
		out.println("* Feed publishing *");

		final FacebookType publishMessageResponse = this.facebookClient.publish("JavaSupport/feed", FacebookType.class,
				Parameter.with("message", "RestFB test"));

		out.println("Published message ID: " + publishMessageResponse.getId());
		return publishMessageResponse.getId();
	}

	String publishEvent() {
		out.println("* Event publishing *");

		final Date tomorrow = new Date(currentTimeMillis() + (1000L * 60L * 60L * 24L));
		final Date twoDaysFromNow = new Date(currentTimeMillis() + (1000L * 60L * 60L * 48L));

		final FacebookType publishEventResponse = this.facebookClient.publish("me/events", FacebookType.class,
				Parameter.with("name", "Party"), Parameter.with("start_time", tomorrow), Parameter.with("end_time", twoDaysFromNow));

		out.println("Published event ID: " + publishEventResponse.getId());
		return publishEventResponse.getId();
	}

	String publishPhoto() {
		out.println("* Binary file publishing *");

		final FacebookType publishPhotoResponse = this.facebookClient.publish("me/photos", FacebookType.class,
				BinaryAttachment.with("cat.png", this.getClass().getResourceAsStream("/cat.png")), Parameter.with("message", "Test cat"));

		out.println("Published photo ID: " + publishPhotoResponse.getId());
		return publishPhotoResponse.getId();
	}

	void delete(final String objectId) {
		out.println("* Object deletion *");
		out.println(format("Deleted %s: %s", objectId, this.facebookClient.deleteObject(objectId)));
	}
}